package cn.wolfcode.luowowo.search.service.impl;

import cn.wolfcode.luowowo.search.query.SearchQueryObject;
import cn.wolfcode.luowowo.search.repository.ScenicspotTemplateRepository;
import cn.wolfcode.luowowo.search.service.IScenicspotTemplateSearchService;
import cn.wolfcode.luowowo.search.template.ScenicspotTemplate;
import cn.wolfcode.luowowo.search.template.StrategyTemplate;
import com.alibaba.dubbo.config.annotation.Service;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;

@Service
public class ScenicspotTemplateSearchServiceImpl implements IScenicspotTemplateSearchService{
    @Autowired
    private ScenicspotTemplateRepository repository;

    public void saveOrUpdate(ScenicspotTemplate scenicspotTemplate) {
        repository.save(scenicspotTemplate);
    }

    public Page<ScenicspotTemplate> list(SearchQueryObject qo) {
        //分页对象
        PageRequest pageable = PageRequest.of(qo.getCurrentPage() - 1, qo.getPageSize());
        //查询条件对象
        BoolQueryBuilder query = QueryBuilders.boolQuery();
        if(qo.getKeyword() != null){
            query.must(QueryBuilders.termQuery("catalog", qo.getKeyword()));
        }
        //按分页条件和查询条件查询出结果
        Page<ScenicspotTemplate> page = repository.search(query, pageable);
        return page;
    }


}
